﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.IO.IsolatedStorage;
using FinosLibrary;
using System.Windows.Media.Imaging;
namespace BattleShipOnline {
    public class Custom {
        public string name;
        public string password;
        public bool usePassword;
        public void upDate() {
            SetCustom(this);
        }
        public static Custom GetCustom() {
            var custom = new Custom() { name = "", password = "", usePassword = false };
            try {
                using (var store = IsolatedStorageFile.GetUserStoreForApplication()) {
                    if (store.FileExists("custom.ini")) {
                        using (var stream = store.OpenFile("custom.ini", FileMode.Open)) {
                            using (StreamReader sr = new StreamReader(stream)) {
                                try {
                                    custom = sr.ReadToEnd().FromJsonTo<Custom>();
                                } catch (Exception) {
                                    SetCustom(custom);
                                }
                            }
                        }
                    }
                }
            } catch (IsolatedStorageException) {
                
            }
            return custom;
        }
        public static void SetCustom(Custom value) {
            try {
                using (var store = IsolatedStorageFile.GetUserStoreForApplication()) {
                    using (var stream = store.OpenFile("custom.ini",FileMode.CreateNew)) {
                        using (StreamWriter sw = new StreamWriter(stream)) {
                            sw.Write(value.ToJson());
                        }
                    }
                }
            } catch (IsolatedStorageException) {
            }
        }
        public static BitmapImage GetBitmapImage(string name) {
            BitmapImage result = new BitmapImage();
            using (var store = IsolatedStorageFile.GetUserStoreForApplication()) {
                if (store.FileExists(name)) {
                    using (var stream = store.OpenFile(name, FileMode.Open)) {
                        result.SetSource(stream);
                    }
                } else {
                    GetResource(name, s => result.SetSource(s));
                }
            }
            return result;
        }
        public static void GetResource(string name,Action<Stream> func) {
            using (var store = IsolatedStorageFile.GetUserStoreForApplication()) {
                if (store.FileExists(name)) {
                    using (var stream = store.OpenFile(name, FileMode.Open)) {
                        func(stream);
                    }
                } else {
                    WebClient wc = new WebClient();
                    wc.OpenReadCompleted += (object sender, OpenReadCompletedEventArgs e) => {
                        using (var store0 = IsolatedStorageFile.GetUserStoreForApplication()) {
                            byte[] bytes = new byte[e.Result.Length];
                            e.Result.Read(bytes, 0, bytes.Length);
                            try {
                                int index = name.LastIndexOf('/');
                                if (index != -1) {
                                    string dir = name.Substring(0, index);
                                    if (store0.GetDirectoryNames(dir).Length == 0) {
                                        store0.CreateDirectory(dir);
                                    }
                                }
                                using (var stream = store0.CreateFile(name)) {
                                    stream.Write(bytes, 0, bytes.Length);
                                }
                            } catch (IsolatedStorageException) {
                                var dialog = IncreaseQuota.ShowDialog();
                                dialog.Closed += (object sender0, EventArgs e0) => {
                                    using (var store00 = IsolatedStorageFile.GetUserStoreForApplication()) {
                                        if (dialog.DialogResult == true) {
                                            using (var stream = store00.CreateFile(name)) {
                                                stream.Write(bytes, 0, bytes.Length);
                                                stream.Seek(0, SeekOrigin.Begin);
                                                func(stream);
                                            }
                                        }
                                    }
                                };
                                e.Result.Close();
                                return;
                            }
                            e.Result.Seek(0, SeekOrigin.Begin);
                            func(e.Result);
                        }
                    };
                    wc.OpenReadAsync(new Uri(name, UriKind.Relative));
                }
            }
        }
    }
}
